﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.BusinessRules;
using Basic4Net.Entities;
using Basic4Net.OAWeb.Base;

namespace Basic4Net.OAWeb.Module_Accounting
{
    public partial class main : BasePage
    {
        private static Amount _amount;
        protected static Amount HAmount
        {
            get { return _amount ?? (_amount = new Amount()); }
        }

        private static AccountingRules _amountRules;
        protected static AccountingRules HAmountRules
        {
            get { return _amountRules ?? (_amountRules = new AccountingRules()); }
        }

        private t_Amount _a = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            _a = HAmount.GetActiveAmount(SchoolMark);

            if (_a != null)
            {
                hidHaveMonth.Value = "0";
                labMsg.Text = String.Format("当前核算月为: {0}年{1}月", _a.FYear, _a.FMonth);
                hidVidify.Value = "0";
                if (_a.Verify.Value == 1)
                {
                    hidVidify.Value = "1";
                    
                }

            }
            else
            {
                hidVidify.Value = "0";
                hidHaveMonth.Value = "1";
            }
            Bind();
        }

        protected void BtnF5Click(object sender, EventArgs e)
        { 
            Bind();
        }

        private void Bind()
        {
            if(_a == null)
                _a = HAmount.GetActiveAmount(SchoolMark);

            if (_a != null && _a.FYear.HasValue && _a.FMonth.HasValue)
            {
                Repeater1.DataSource = HAmountRules.GetCurrentMonthRules(_a.FYear.Value, _a.FMonth.Value, SchoolMark);
                Repeater1.DataBind();
            }

        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        [System.Web.Services.WebMethod]
        public static string SaveRules(List<Hashtable> arr)
        {
            string strmsg = String.Empty;
            t_Amount t = HAmount.GetActiveAmount(SchoolMark);
            IList<t_AccountingRules> ruleList = new List<t_AccountingRules>();
            foreach (Hashtable o in arr)
            {
                int rid;
                int actualWorkload;
                int attendance;
                int safety;
                int deductAtt;
                int deductSaf;
                int subsidyCls;
                int objectRules;
                Int32.TryParse(o["RID"].ToString(), out rid);
                Int32.TryParse(o["ActualWorkload"].ToString(), out actualWorkload);
                Int32.TryParse(o["Attendance"].ToString(), out attendance);
                Int32.TryParse(o["Safety"].ToString(), out safety);
                Int32.TryParse(o["DeductAtt"].ToString(), out deductAtt);
                Int32.TryParse(o["DeductSaf"].ToString(), out deductSaf);
                Int32.TryParse(o["SubsidyCls"].ToString(), out subsidyCls);
                Int32.TryParse(o["ObjectRules"].ToString(), out objectRules);

                t_AccountingRules ar = new t_AccountingRules{RID = rid, FYear = t.FYear,FMonth = t.FMonth,
                ActualWorkload = actualWorkload, Attendance = attendance, 
                Safety = safety, DeductAtt = deductAtt, DeductSaf = deductSaf,
                SubsidyCls = subsidyCls, SchoolMark = SchoolMark, ObjectRules = objectRules};

                ruleList.Add(ar);
            }

            HAmountRules.SaveRules(ruleList, ref strmsg);

            return strmsg;
        }
    }
}
